package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description NursingServiceImpl
 * @Author chenchuang
 * @Date 2024-08-25
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    /**
     * 分页查询护理计划
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.selectByPage(name, status);
        PageResponse<NursingPlanVo> pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 新增护理计划
     * @param nursingPlanDto
     */
    @Transactional
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan plan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        plan.setStatus(1);
        nursingPlanMapper.insert(plan);
        nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(plan.getId()));
        nursingProjectPlanService.addList(nursingPlanDto.getProjectPlans());

    }

    /**
     * 根据id查询护理计划
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        return nursingPlanVo;
    }

    @Transactional
    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        Long id = nursingPlanDto.getId();
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        nursingPlanVo.getProjectPlans().forEach(v -> nursingProjectMapper.updateCount1(v.getProjectId()));
        nursingProjectPlanService.delete(id);
        if (CollUtil.isNotEmpty(nursingPlanDto.getProjectPlans())) {
            nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(nursingPlanDto.getId()));
            nursingProjectPlanService.addList(nursingPlanDto.getProjectPlans());
        }
        nursingPlanMapper.update(nursingPlan);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        nursingPlanVo.getProjectPlans().forEach(v -> nursingProjectMapper.updateCount1(v.getProjectId()));
        nursingProjectPlanService.delete(id);
        nursingPlanMapper.detele(id);
    }

    @Override
    public List<NursingPlan> getAll() {
        return nursingPlanMapper.getAll();
    }

    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }
}
